package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.PropertyBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.PropertyBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Controller
@RequestMapping("/propertyController")
public class PropertyController {
    
	@Autowired private DictionaryBiz dictionaryBiz;
	
	@Autowired private PropertyBiz propertyBiz;
	
	@RequestMapping("/toPage")
	public ModelAndView toPage(){
		ModelAndView view=new ModelAndView("lm/property/propertyList");
		view.addObject("propertyTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_TYPE.getCode()));
		view.addObject("propertyStateList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_STATE.getCode()));
		view.addObject("placeList", this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_BASE.getCode()));
		view.addObject("userTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.USER_TYPE.getCode()));
		return view;
	}

	@RequestMapping("/toLoanReturnPage")
	public ModelAndView toLoanReturnPage(){
		List<Integer> idList=new ArrayList<Integer>(); 
		idList.add(DictionaryEnums.IN_USE.getCode());
		idList.add(DictionaryEnums.UNUSE.getCode());

		ModelAndView view=new ModelAndView("lm/propertyLoanReturn/propertyList");
		view.addObject("propertyTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_TYPE.getCode()));
		view.addObject("propertyStateList", this.dictionaryBiz.queryDictionaryByIds(idList));
		view.addObject("placeList", this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_BASE.getCode()));
		view.addObject("userType", DictionaryEnums.STUDENT_USER_TYPE.getCode());
		return view;
	}
	
	@RequestMapping("/toInsertPage")
	public ModelAndView toInsertPage(){
		ModelAndView view=new ModelAndView("lm/property/propertyAdd");
		view.addObject("propertyTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_TYPE.getCode()));
		view.addObject("propertyStateList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_STATE.getCode()));
		view.addObject("placeList", this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_BASE.getCode()));
		view.addObject("userTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.USER_TYPE.getCode()));
		return view;
	}
	
	@RequestMapping("/toEditPage")
	public ModelAndView toEditPage(@RequestParam String propertyId){
		ModelAndView view=new ModelAndView("lm/property/propertyEdit");
		view.addObject("propertyBean", this.propertyBiz.getBeanByKey(propertyId));
		view.addObject("propertyTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_TYPE.getCode()));
		view.addObject("propertyStateList", this.dictionaryBiz.getListByParent(DictionaryEnums.PROPERTY_STATE.getCode()));
		view.addObject("placeList", this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_BASE.getCode()));
		view.addObject("userTypeList", this.dictionaryBiz.getListByParent(DictionaryEnums.USER_TYPE.getCode()));
		return view;
	}
	
	@RequestMapping("/insertProperty")
	@ResponseBody
	public String insertProperty(PropertyBean propertyBean){
		CommonResult result = this.propertyBiz.insertBean(propertyBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/updateProperty")
	@ResponseBody
	public String updateProperty(PropertyBean propertyBean){
		CommonResult result=this.propertyBiz.updateBean(propertyBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/deleteProperty")
	@ResponseBody
	public String deleteProperty(@RequestBody List<String> idList){
		CommonResult result=this.propertyBiz.deleteByList(idList);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/getList")
	@ResponseBody
	public String getList(CommonPage page){
		this.propertyBiz.getListByPage(page);
		return page.toSHowPage();
	}
}
